<?php
namespace app\m\controller;

use app\common\controller\FrontController;
use app\common\service\GoodsService;
use app\common\service\MemberService;
use app\common\service\OrderService;
use think\facade\Request;

class Order extends FrontController
{
    public function index(){
        $status = input('status',0);

        if (request()->isAjax()) {
            $orderService = new OrderService();
            $param = input('param.');
            $where = [];
            $pageIndex = 1;
            if (!empty($param['status'])) {
                $where['status'] = $param['status'];
            }
            if (!empty($param['p'])) {
                $pageIndex = $param['p'];
            }
            $jieguo = $orderService->getOrderPager($this->getMemberId(),$where,$pageIndex);
            if($jieguo['err_code'] != 0){
                $this->error($jieguo['msg']);
            }
            else{
                $this->success('成功', '', $jieguo['data']);
            }
        }

        $this->assign('status', $status);
        return $this->fetch();
    }

    public function create()
    {
        // 2 购物车 ； 1 商品详情点击购买
        // 购物车 ID ，如果没有则使用商品ID
        $cartIds = input('cart_ids');
        if (!empty($cartIds)) {
            // 用购物车ID获取商品ID
            $type = 2;
            $ids = $cartIds;
        }
        else{
            $type = 1;
            // 商品ID
            $ids = input('ids');
        }
        $goodsOptionIds = input('option_ids');
        $totals = input('total');
        $goodsService = new GoodsService();
        if(!empty($goodsOptionIds)){
            $goodsOptionIds = explode(',', $goodsOptionIds);
        }
        // 如果有提交数量的话
        if (!empty($totals)) {
            $totals = explode(',', $totals);
        }

        // 商品 商品只有一个
        if(1 == $type){
            $jieguo = $goodsService->getGoods($ids);
            if($jieguo['err_code'] != 0){
                $this->error($jieguo['msg']);
            }
            $goodsModel = $jieguo['data'];
            $goodsModel['option_id'] = $goodsOptionIds[0];
            $goodsModel['cart_total'] = $totals[0];
            $goodsList = [$goodsModel];
        }
        else{
            // 购物车

            // 取出商品信息
            $jieguo = $goodsService->getGoodssByCartIds($ids,$type);
            if($jieguo['err_code'] != 0){
                $this->error($jieguo['msg']);
            }
            $goodsList = $jieguo['data'];
            if(count($goodsList) < 1){
                $this->error('没有找到商品');
            }
        }

        $this->assign('goodsList',$goodsList);

        $goodsMap = [];

        foreach ($goodsList as $key => $item) {
            $tmp = [
                'goodsid' => (1 == $type ? $item['id']:$item['goods_id']),
                'cartid' => (empty($item['cart_id']) ? '0' : $item['cart_id']),
            ];

            if(1 == $type){
                $tmp['option_id'] = $goodsOptionIds[$key];
                $tmp['total'] = $totals[$key];
            }
            else{
                $tmp['option_id'] = (empty($item['option_id']) ? '1' : $item['option_id']);
                $tmp['total'] = (empty($item['cart_total']) ? '1' : $item['cart_total']);
            }

            $goodsMap[] = $tmp;
        }

        $this->assign('goodsMap', json_encode($goodsMap,256));

        // 取出当前登录会员的默认收货地址
        $memberService = new MemberService();
        $jieguo = $memberService->getDefaultAddress($this->getMemberId());
        if(1 == $jieguo['err_code']){
            $this->error($jieguo['msg']);
        }
        if(1012 == $jieguo['err_code']){
            $this->assign('noAddress',1);
        }
        else{
            $this->assign('noAddress',0);
        }
        if(0 == $jieguo['err_code']){
            $this->assign('address',$jieguo['data']);
        }

        return $this->fetch();
    }

    /**
     * 提交订单，ajax方式
     */
    public function submit_order(){
        $goods = input('goods');
        $address_id = input('address_id');
        $remark = input('remark');
        if (empty($address_id)) {
            $this->error('地址参数不正确，请刷新重新提交');
        }
        $json = json_decode($goods, true);
        if(!$json){
            $this->error('商品参数错误，请刷新重新提交');
        }

        $orderService = new OrderService();
        $jieguo = $orderService->submitOrder($this->getMemberId(), $address_id, $json,$remark);
        if($jieguo['err_code'] != 0){
            $this->error($jieguo['msg']);
        }
        else{
            $this->success('提交订单成功', '', $jieguo['data']);
        }
    }

    public function pay(){
        $orderId = input('id');
        if (empty($orderId)) {
            $this->error('订单ID不正确');
        }

        $orderService = new OrderService();
        $jieguo = $orderService->getOrder($this->getMemberId(),$orderId);
        if($jieguo['err_code'] != 0){
            $this->error('查询订单失败');
        }
        $orderModel = $jieguo['data'];

        $this->assign('orderModel',$orderModel);
        return $this->fetch();
    }

    public function del_order(){
        $orderId = input('id');
        if (empty($orderId)) {
            $this->error('订单ID不正确');
        }

        $orderService = new OrderService();
        $jieguo = $orderService->delOrder($this->getMemberId(), $orderId);
        if($jieguo['err_code'] != 0){
            $this->error('删除订单失败');
        }
        else{
            $this->success('删除订单成功');
        }
    }

    public function cancel_order(){
        $orderId = input('id');
        $remark = input('remark','');
        if (empty($orderId)) {
            $this->error('订单ID不正确');
        }

        $orderService = new OrderService();
        $jieguo = $orderService->cancelOrder($this->getMemberId(), $orderId,$remark);
        if($jieguo['err_code'] != 0){
            $this->error('取消订单失败');
        }
        else{
            $this->success('取消订单成功');
        }
    }

    public function finish_order(){
        $orderId = input('id');
        if (empty($orderId)) {
            $this->error('订单ID不正确');
        }

        $orderService = new OrderService();
        $jieguo = $orderService->finishOrder($this->getMemberId(), $orderId);
        if($jieguo['err_code'] != 0){
            $this->error('确认收货失败');
        }
        else{
            $this->success('确认收货成功');
        }
    }

    public function express(){
        $orderId = input('id');
        if (empty($orderId)) {
            $this->error('订单ID不正确');
        }
        $orderService = new OrderService();
        $jieguo = $orderService->getOrder($this->getMemberId(),$orderId);
        if($jieguo['err_code'] != 0){
            $this->error('查询订单失败');
        }
        $orderModel = $jieguo['data'];
        $this->assign('orderModel', $orderModel);

        $jieguo = $orderService->expressByExpressSn($orderModel['oe_express_sn'],$orderModel['oe_express']);
        if($jieguo['err_code'] != 0){
            $this->error($jieguo['msg']);
        }

        $this->assign('expressInfo', $jieguo['data']);

        return $this->fetch();
    }

    public function detail(){
        $orderId = input('id');
        if (empty($orderId)) {
            $this->error('订单ID不正确');
        }

        $orderService = new OrderService();
        $jieguo = $orderService->getOrder($this->getMemberId(),$orderId);
        if($jieguo['err_code'] != 0){
            $this->error('查询订单失败');
        }
        $orderModel = $jieguo['data'];
//        dump($orderModel);exit;
        $this->assign('orderModel', $orderModel);

        // 默认 specs
        $this->assign('defaultSpecs', config('site.goods_option_specs_val'));

        return $this->fetch();
    }
}
